import math
import random
from numpy.core.numeric import zeros

# Constantes
epsilon = 0.00001
N = 10

#Clase.
class Estacionario:
	def __init__(self,matriz):
		"""Constructor de Clase"""
		self.matriz = matriz
		self.v = self.inic_v()
		self.vacum = self.inic_vacum()
		self.macum = self.inic_macum()
		self.paso = 0
		
	def inic_v(self):
		v = []
		for i in range(N):
			v.append( 1 / float(N) )
		return v

	def inic_vacum(self):
		vacum = []
		vacum.append(self.v[0])
		for i in range(N):
			vacum.append(vacum[i] + self.v[i])
		return vacum
	
	def inic_macum(self):
		macum = zeros((N,N))
		for j in range(N):
			macum[0][j] = self.matriz[0][j];
			for i in range(1,N):
				macum[i][j] = (self.matriz[i][j] + macum[i-1][j]);
		return macum
			
	def primer_paso(self):
		return 0
	
	# calcula el vector estacionario ve
	def calcular_ve(self):
		paso = 0
		esq = self.primer_paso() # estado/esquina inicial
		hits = zeros(N) # cant de veces (hits) que pasé por cada esquina
		v = zeros(N) # vector a retornar
		while not self.converge(V,V_ant):
			paso += 1
			v[esq] = hits[esq] / paso
			esquina = self.sig_dado_ant(x)
			hist[esq] += 1;
		return v

	def calcular_ve2(self, t):
		tray = 0
		x = self.primer_paso() # estado inicial
		Vt = zeros(N) # vector trayectoria
		V = zeros(N) # vector actual
		V_ant = [-1., 0., 0., 0., 0., 0., 0., 0., 0., 0.] # vector anterior
		while not self.converge(V,V_ant):
			tray = tray + 1
			for i in range(t):
				x = self.sig_dado_ant(x)
			Vt[x] = Vt[x] + 1;
			V_ant = V
			for p in range(N):
				V[p] = Vt[p] / tray
				
		return V

	def converge(self, actual, anterior):
		for i in range(N):
			if (abs(actual[i] - anterior[i])>epsilon):
				return False
		return True

	def sig_dado_ant(self,j):
		r = random.random()
		for i in range(N):
			if (r < self.macum[i][j]):
				return i
	
# Comienzo de Drno

drno = [
	[0,0.15,0,0,0,0,0,0,0,0.7],
	[1.0,0,0.3,0,0.3,0,0,0,0,0],
	[0,0.15,0,1.0,0,0,0,0,0,0],
	[0,0,0.7,0,0,0,0,0,0,0],
	[0,0.7,0,0,0,0,0.15,0,0,0],
	[0,0,0,0,0,0,0.15,0,0,0],
	[0,0,0,0,0.7,1.0,0,0.15,0,0],
	[0,0,0,0,0,0,0.7,0,1.0,0.3],
	[0,0,0,0,0,0,0,0.15,0,0],
	[0,0,0,0,0,0,0,0.7,0,0],
]	

bond = [
	[0,0.33,0,0,0,0,0,0,0,0.5],
	[1,0,0.5,0,0.5,0,0,0,0,0],
	[0,0.33,0,1,0,0,0,0,0,0],
	[0,0,0.5,0,0,0,0,0,0,0],
	[0,0.33,0,0,0,0,0.33,0,0,0],
	[0,0,0,0,0,0,0.33,0,0,0],
	[0,0,0,0,0.5,1,0,0.33,0,0],
	[0,0,0,0,0,0,0.33,0,1,0.5],
	[0,0,0,0,0,0,0,0.33,0,0],
	[0,0,0,0,0,0,0,0.33,0,0]
]

p = Estacionario(drno)
#print "Vector V"
#print p.v

#print "Vector Acumulado"
print p.vacum

#print drno[1][0]
print p.macum

print "v*", p.calcular_ve()
